package com.douglas.managesystem.service.student.impl;

import com.douglas.managesystem.common.RoleTypeEnum;
import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.dao.student.StudentMapper;
import com.douglas.managesystem.entity.studentpojo.StuRole;
import com.douglas.managesystem.entity.studentpojo.Student;
import com.douglas.managesystem.service.student.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 学生业务处理接口实现
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-15 18:00
 **/
@SuppressWarnings("all")
@Service
public class StudentServiceImpl implements StudentService {

    /**
     * 学生数据处理mapper
     */
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 1.分页查询学生信息
     * @param params
     * @return
     */
    @Override
    public PageInfo<Student> findAllStuByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Student> list = studentMapper.queryListByPage(params.getEntity());
        PageInfo<Student> page = new PageInfo<Student>(list);
        return page;
    }

    /**
     * 2.新增学生信息
     * @param student
     */
    @Override
    public void saveStudent(Student student) {
        Student studentDb = studentMapper.queryStuByStuNumber(student.getStuNumber());
        if(studentDb != null){
            return;
        }
        // 新增学生角色信息到 用户角色表
        StuRole stuRole = new StuRole();
        stuRole.setStuNumber(student.getStuNumber());
        stuRole.setRoleType(RoleTypeEnum.STUDENT.getCode());
        // 1.设置新增时间
        student.setCreatedTime(new Date());
        // 2.用户密码加密的盐
        ByteSource salt = ByteSource.Util.bytes(student.getStuNumber() + "123456");
        // 3.加盐后的密码
        SimpleHash saltPassword = new SimpleHash("MD5", "123456", salt, 1024);
        // 4.设置初始加密密码
        student.setPassword(saltPassword.toString());
        // 5.新增数据
        studentMapper.insert(student);
        // 6.角色表添加当前学生的角色
        studentMapper.insertStuRole(stuRole);

    }

    /**
     * 3.更新学生信息
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        // 1.设置更新时间
        student.setUpdatedTime(new Date());
        // 2.更新数据
        studentMapper.update(student);
    }

    /**
     * 4.跟id查询学生对象
     * @param id
     * @return
     */
    @Override
    public Student findStuById(String id) {

        return studentMapper.queryStuById(id);
    }

    /**
     * 5.根据id删除学生
     * @param id
     */
    @Override
    public void deleteStudent(String id) {
        studentMapper.delete(id);
    }

    /**
     * 6.根据stu_number查询学生对象
     * @param id
     * @return
     */
    @Override
    public Student findStuByStuNumber(String id) {
        return studentMapper.queryStuByStuNumber(id);
    }
}
